/*
* Degrees.java
* Created on  202015/6/12 20:03
* 版本       修改时间          作者      修改内容
* V1.0.1    202015/6/12       panzhuowen    初始版本
*
*/

package com.infitecs.eyas.util;

import fr.expression4j.basic.MathematicalElement;
import fr.expression4j.basic.OperatorManager;
import fr.expression4j.core.Catalog;
import fr.expression4j.core.ExpressionModel;
import fr.expression4j.core.Parameters;
import fr.expression4j.core.exception.EvalException;
import fr.expression4j.core.exception.ParametersException;
import fr.expression4j.core.predefine.AbstractFunction;
import fr.expression4j.factory.ExpressionFactory;
import fr.expression4j.factory.NumberFactory;
import fr.expression4j.util.ParameterUtil;

import java.util.List;

/**
 * 类的描述信息
 *
 * @author panzhuowen
 * @version 1.0.1
 */
public class Functions {

    public static class DEGREE extends AbstractFunction {
        /**
         * compute the factoriel of a number
         *
         * @param x number to compute
         * @return factoriel of the number.
         */
        private double DEGREES(double x) {
            return Math.toDegrees(x);
        }

        /**
         * evaluation method called by the Expression4j when needed.
         *
         * @param parameters parameter given to the function for the evaluation.
         */
        public MathematicalElement evaluate(Parameters parameters)
                throws EvalException {
            try {
                MathematicalElement me = parameters.getParameter("x");
                double tmpValue = me.getRealValue();
                double result = DEGREES(tmpValue);
                return NumberFactory.createReal(result);
            } catch (ParametersException pe) {
                throw new EvalException("Cannot evaluate fact(x). " + pe);
            }
        }

        public MathematicalElement evaluate(OperatorManager operatorManager,
                                            Parameters parameters) throws EvalException {
            return evaluate(parameters);
        }

        public Catalog getCatalog() {
            return ExpressionFactory.getCatalog();
        }

        /**
         * get the function name
         */
        public String getName() {
            return "DEGREES";
        }

        /**
         * get parameter list of the function.
         */
        public List getParameters() {
            //this util method get one parameter "x".
            return ParameterUtil.generateXParameters();
        }

        public ExpressionModel getExpressionModel() {
            // TODO Auto-generated method stub
            return null;
        }
    }

    public static class ATANS extends AbstractFunction {
        /**
         * compute the factoriel of a number
         *
         * @param x number to compute
         * @return factoriel of the number.
         */
        private double ATAN(double x) {
            return Math.atan(x);
        }

        /**
         * evaluation method called by the Expression4j when needed.
         *
         * @param parameters parameter given to the function for the evaluation.
         */
        public MathematicalElement evaluate(Parameters parameters)
                throws EvalException {
            try {
                MathematicalElement me = parameters.getParameter("x");
                double tmpValue = me.getRealValue();
                double result = ATAN(tmpValue);
                return NumberFactory.createReal(result);
            } catch (ParametersException pe) {
                throw new EvalException("Cannot evaluate fact(x). " + pe);
            }
        }

        public MathematicalElement evaluate(OperatorManager operatorManager,
                                            Parameters parameters) throws EvalException {
            return evaluate(parameters);
        }

        public Catalog getCatalog() {
            return ExpressionFactory.getCatalog();
        }

        /**
         * get the function name
         */
        public String getName() {
            return "ATAN";
        }

        /**
         * get parameter list of the function.
         */
        public List getParameters() {
            //this util method get one parameter "x".
            return ParameterUtil.generateXParameters();
        }

        public ExpressionModel getExpressionModel() {
            // TODO Auto-generated method stub
            return null;
        }
    }

    public static class LOG extends AbstractFunction {
        /**
         * compute the factoriel of a number
         *
         * @param x number to compute
         * @return factoriel of the number.
         */
        private double LN(double x) {
            return Math.log(x);
        }

        /**
         * evaluation method called by the Expression4j when needed.
         *
         * @param parameters parameter given to the function for the evaluation.
         */
        public MathematicalElement evaluate(Parameters parameters)
                throws EvalException {
            try {
                MathematicalElement me = parameters.getParameter("x");
                double tmpValue = me.getRealValue();
                double result = LN(tmpValue);
                return NumberFactory.createReal(result);
            } catch (ParametersException pe) {
                throw new EvalException("Cannot evaluate fact(x). " + pe);
            }
        }

        public MathematicalElement evaluate(OperatorManager operatorManager,
                                            Parameters parameters) throws EvalException {
            return evaluate(parameters);
        }

        public Catalog getCatalog() {
            return ExpressionFactory.getCatalog();
        }

        /**
         * get the function name
         */
        public String getName() {
            return "LN";
        }

        /**
         * get parameter list of the function.
         */
        public List getParameters() {
            //this util method get one parameter "x".
            return ParameterUtil.generateXParameters();
        }

        public ExpressionModel getExpressionModel() {
            // TODO Auto-generated method stub
            return null;
        }
    }

    public static class LOG10FUN extends AbstractFunction {
        /**
         * compute the factoriel of a number
         *
         * @param x number to compute
         * @return factoriel of the number.
         */
        private double LOG10(double x) {
            return Math.log10(x);
        }

        /**
         * evaluation method called by the Expression4j when needed.
         *
         * @param parameters parameter given to the function for the evaluation.
         */
        public MathematicalElement evaluate(Parameters parameters)
                throws EvalException {
            try {
                MathematicalElement me = parameters.getParameter("x");
                double tmpValue = me.getRealValue();
                double result = LOG10(tmpValue);
                return NumberFactory.createReal(result);
            } catch (ParametersException pe) {
                throw new EvalException("Cannot evaluate fact(x). " + pe);
            }
        }

        public MathematicalElement evaluate(OperatorManager operatorManager,
                                            Parameters parameters) throws EvalException {
            return evaluate(parameters);
        }

        public Catalog getCatalog() {
            return ExpressionFactory.getCatalog();
        }

        /**
         * get the function name
         */
        public String getName() {
            return "LOG10";
        }

        /**
         * get parameter list of the function.
         */
        public List getParameters() {
            //this util method get one parameter "x".
            return ParameterUtil.generateXParameters();
        }

        public ExpressionModel getExpressionModel() {
            // TODO Auto-generated method stub
            return null;
        }
    }

    public static class LOGFUN extends AbstractFunction {
        /**
         * compute the factoriel of a number
         *
         * @param x number to compute
         * @return factoriel of the number.
         */
        private double LOG(double x) {
            return Math.log10(x);
        }

        /**
         * evaluation method called by the Expression4j when needed.
         *
         * @param parameters parameter given to the function for the evaluation.
         */
        public MathematicalElement evaluate(Parameters parameters)
                throws EvalException {
            try {
                MathematicalElement me = parameters.getParameter("x");
                double tmpValue = me.getRealValue();
                double result = LOG(tmpValue);
                return NumberFactory.createReal(result);
            } catch (ParametersException pe) {
                throw new EvalException("Cannot evaluate fact(x). " + pe);
            }
        }

        public MathematicalElement evaluate(OperatorManager operatorManager,
                                            Parameters parameters) throws EvalException {
            return evaluate(parameters);
        }

        public Catalog getCatalog() {
            return ExpressionFactory.getCatalog();
        }

        /**
         * get the function name
         */
        public String getName() {
            return "LOG";
        }

        /**
         * get parameter list of the function.
         */
        public List getParameters() {
            //this util method get one parameter "x".
            return ParameterUtil.generateXParameters();
        }

        public ExpressionModel getExpressionModel() {
            // TODO Auto-generated method stub
            return null;
        }
    }

    public static class EXPFUN extends AbstractFunction {
        /**
         * compute the factoriel of a number
         *
         * @param x number to compute
         * @return factoriel of the number.
         */
        private double EXP(double x) {
            return Math.exp(x);
        }

        /**
         * evaluation method called by the Expression4j when needed.
         *
         * @param parameters parameter given to the function for the evaluation.
         */
        public MathematicalElement evaluate(Parameters parameters)
                throws EvalException {
            try {
                MathematicalElement me = parameters.getParameter("x");
                double tmpValue = me.getRealValue();
                double result = EXP(tmpValue);
                return NumberFactory.createReal(result);
            } catch (ParametersException pe) {
                throw new EvalException("Cannot evaluate fact(x). " + pe);
            }
        }

        public MathematicalElement evaluate(OperatorManager operatorManager,
                                            Parameters parameters) throws EvalException {
            return evaluate(parameters);
        }

        public Catalog getCatalog() {
            return ExpressionFactory.getCatalog();
        }

        /**
         * get the function name
         */
        public String getName() {
            return "EXP";
        }

        /**
         * get parameter list of the function.
         */
        public List getParameters() {
            //this util method get one parameter "x".
            return ParameterUtil.generateXParameters();
        }

        public ExpressionModel getExpressionModel() {
            // TODO Auto-generated method stub
            return null;
        }
    }

    public static class SQRTFUN extends AbstractFunction {
        /**
         * compute the factoriel of a number
         *
         * @param x number to compute
         * @return factoriel of the number.
         */
        private double sqrt(double x) {
            return Math.sqrt(x);
        }

        /**
         * evaluation method called by the Expression4j when needed.
         *
         * @param parameters parameter given to the function for the evaluation.
         */
        public MathematicalElement evaluate(Parameters parameters)
                throws EvalException {
            try {
                MathematicalElement me = parameters.getParameter("x");
                double tmpValue = me.getRealValue();
                double result = sqrt(tmpValue);
                return NumberFactory.createReal(result);
            } catch (ParametersException pe) {
                throw new EvalException("Cannot evaluate fact(x). " + pe);
            }
        }

        public MathematicalElement evaluate(OperatorManager operatorManager,
                                            Parameters parameters) throws EvalException {
            return evaluate(parameters);
        }

        public Catalog getCatalog() {
            return ExpressionFactory.getCatalog();
        }

        /**
         * get the function name
         */
        public String getName() {
            return "sqrt";
        }

        /**
         * get parameter list of the function.
         */
        public List getParameters() {
            //this util method get one parameter "x".
            return ParameterUtil.generateXParameters();
        }

        public ExpressionModel getExpressionModel() {
            // TODO Auto-generated method stub
            return null;
        }
    }

    public static class SINFUN extends AbstractFunction {
        /**
         * compute the factoriel of a number
         *
         * @param x number to compute
         * @return factoriel of the number.
         */
        private double SIN(double x) {
            return Math.sin(x);
        }

        /**
         * evaluation method called by the Expression4j when needed.
         *
         * @param parameters parameter given to the function for the evaluation.
         */
        public MathematicalElement evaluate(Parameters parameters)
                throws EvalException {
            try {
                MathematicalElement me = parameters.getParameter("x");
                double tmpValue = me.getRealValue();
                double result = SIN(tmpValue);
                return NumberFactory.createReal(result);
            } catch (ParametersException pe) {
                throw new EvalException("Cannot evaluate fact(x). " + pe);
            }
        }

        public MathematicalElement evaluate(OperatorManager operatorManager,
                                            Parameters parameters) throws EvalException {
            return evaluate(parameters);
        }

        public Catalog getCatalog() {
            return ExpressionFactory.getCatalog();
        }

        /**
         * get the function name
         */
        public String getName() {
            return "SIN";
        }

        /**
         * get parameter list of the function.
         */
        public List getParameters() {
            //this util method get one parameter "x".
            return ParameterUtil.generateXParameters();
        }

        public ExpressionModel getExpressionModel() {
            // TODO Auto-generated method stub
            return null;
        }
    }

    public static class TANFUN extends AbstractFunction {
        /**
         * compute the factoriel of a number
         *
         * @param x number to compute
         * @return factoriel of the number.
         */
        private double SIN(double x) {
            return Math.tan(x);
        }

        /**
         * evaluation method called by the Expression4j when needed.
         *
         * @param parameters parameter given to the function for the evaluation.
         */
        public MathematicalElement evaluate(Parameters parameters)
                throws EvalException {
            try {
                MathematicalElement me = parameters.getParameter("x");
                double tmpValue = me.getRealValue();
                double result = SIN(tmpValue);
                return NumberFactory.createReal(result);
            } catch (ParametersException pe) {
                throw new EvalException("Cannot evaluate fact(x). " + pe);
            }
        }

        public MathematicalElement evaluate(OperatorManager operatorManager,
                                            Parameters parameters) throws EvalException {
            return evaluate(parameters);
        }

        public Catalog getCatalog() {
            return ExpressionFactory.getCatalog();
        }

        /**
         * get the function name
         */
        public String getName() {
            return "TAN";
        }

        /**
         * get parameter list of the function.
         */
        public List getParameters() {
            //this util method get one parameter "x".
            return ParameterUtil.generateXParameters();
        }

        public ExpressionModel getExpressionModel() {
            // TODO Auto-generated method stub
            return null;
        }
    }


    public static class TANHFUN extends AbstractFunction {
        /**
         * compute the factoriel of a number
         *
         * @param x number to compute
         * @return factoriel of the number.
         */
        private double tanh(double x) {
            return Math.tanh(x);
        }

        /**
         * evaluation method called by the Expression4j when needed.
         *
         * @param parameters parameter given to the function for the evaluation.
         */
        public MathematicalElement evaluate(Parameters parameters)
                throws EvalException {
            try {
                MathematicalElement me = parameters.getParameter("x");
                double tmpValue = me.getRealValue();
                double result = tanh(tmpValue);
                return NumberFactory.createReal(result);
            } catch (ParametersException pe) {
                throw new EvalException("Cannot evaluate fact(x). " + pe);
            }
        }

        public MathematicalElement evaluate(OperatorManager operatorManager,
                                            Parameters parameters) throws EvalException {
            return evaluate(parameters);
        }

        public Catalog getCatalog() {
            return ExpressionFactory.getCatalog();
        }

        /**
         * get the function name
         */
        public String getName() {
            return "TANH";
        }

        /**
         * get parameter list of the function.
         */
        public List getParameters() {
            //this util method get one parameter "x".
            return ParameterUtil.generateXParameters();
        }

        public ExpressionModel getExpressionModel() {
            // TODO Auto-generated method stub
            return null;
        }
    }

}
